package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.WebApplicantIndustryInfo;
import com.baitengsoft.patentapimonitor.api.model.WebApplicantInfo;
import com.baitengsoft.patentapimonitor.api.model.WebApplicantTypeInfo;
import com.baitengsoft.patentapimonitor.api.model.dto.WebApplicantIndustryInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebApplicantInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebApplicantTypeInfoDto;
import com.baitengsoft.patentapimonitor.api.model.param.SearchApplicantAnalysisParam;
import com.baitengsoft.patentapimonitor.api.service.RptApplicantService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 权利人统计导出
 * @author Chen 2018/1/5
 */
@RestController
@RequestMapping("/api/v1/web/analysisexport")
@Api(value = "前台-统计分析-权利人统计导出",description = "权利人统计导出")
public class WebApplicantExportController extends AbstractController{
    @Autowired
    private RptApplicantService rptApplicantService;
    @Autowired
    private WebExcelExportService webExcelExportService;

    /**
     * 权利人统计
     */
    @ApiOperation(value ="权利人统计")
    @GetMapping("/applicantAnalysis")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "dataRange", value = "数据范围  1.当前有效量，2，专利总量，3，今年公开量，4，今年授权量", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantName", value = "权利人名字", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "industryId", value = "行业Id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "type", name = "type", value = "1,企业，2个人，3，院校，4科研机构，5机关团体，6其他", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = false)

    })
    public Res getApplicantAnalysisList(@ApiIgnore SearchApplicantAnalysisParam param,HttpServletResponse response) throws Exception{
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        if (param.getPageSize() > 5000) return Res.error("最多导出5000条");
        PageResult<WebApplicantInfo> applicantAnalysisList = rptApplicantService.getApplicantAnalysisList(userInfo.getPlatformid(), param);
        List<WebApplicantInfo> infos = applicantAnalysisList.getItems();
        List<WebApplicantInfoDto> webDtos = webExcelExportService.getWebApplicantInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "权利人统计.xls",
                "权利人统计");
    }

    /**
     * 权力人行业分布
     */
    @ApiOperation(value ="权力人行业分布")
    @GetMapping("/applicantIndustry")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "dataTypeValue", value = "数据类型", required = true)
    })
    public Res getApplicantIndustry(Integer dataTypeValue,HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        if(dataTypeValue == null)
            return Res.error("数据类型不能为空");
        //获取数据范围
        List<WebApplicantIndustryInfo> infos = rptApplicantService.getApplicantIndustry(userInfo.getPlatformid(),dataTypeValue);
        List<WebApplicantIndustryInfoDto> webDtos = webExcelExportService.getWebApplicantIndustryInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "权利人行业分布.xls",
                "权利人行业分布");
    }

    /**
     * 权力人类型分布
     */
    @ApiOperation(value ="权力人类型分布")
    @GetMapping("/applicantType")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "dataTypeValue", value = "数据类型", required = true)
    })
    public Res getApplicantType(Integer dataTypeValue,HttpServletResponse response ){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        if(dataTypeValue == null)
            return Res.error("数据类型不能为空，权力人专利量");
        //获取权力人总数（全部权力人）
        //获取数据范围
        List<WebApplicantTypeInfo> infos = rptApplicantService.getApplicantType(userInfo.getPlatformid(),dataTypeValue);
        List<WebApplicantTypeInfoDto> webDtos = webExcelExportService.getWebApplicantTypeInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "权力人类型分布.xls",
                "权力人类型分布");
    }
}
